package net.huashitong.scheduling.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.huashitong.Indicators.databean.IndicatorBreakdownDatabean;
import net.huashitong.Indicators.entity.IndicatorBreakdown;
import net.huashitong.Indicators.filter.IndicatorBreakdownFilter;
import net.huashitong.Indicators.service.IndicatorBreakdownService;
import net.huashitong.scheduling.dao.IndicatorSchedulingDao;
import net.huashitong.scheduling.entity.IndicatorScheduling;
import net.huashitong.scheduling.filter.IndicatorSchedulingFilter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;

@Service
public class IndicatorSchedulingService extends StringPKService<IndicatorScheduling> {

	@Autowired
	private IndicatorSchedulingDao dao;
	@Autowired
	private IndicatorBreakdownService breakdownService;
	@Override
	protected BaseDao<IndicatorScheduling, String> getDao() {
		// TODO Auto-generated method stub
		return dao;
	}
	public List<IndicatorScheduling> queryByCategory(
			IndicatorSchedulingFilter filter) {
		return dao.queryByCategory(filter);
	}
	public List<Map<String, Object>> queryShopping(
			IndicatorBreakdownFilter filter) {
		List<Map<String, Object>> maps = new ArrayList<Map<String,Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> map2 = new HashMap<String, Object>();
		List<IndicatorBreakdownDatabean> breakdown2s = new ArrayList<IndicatorBreakdownDatabean>();
		List<IndicatorBreakdownDatabean> breakdown3s = new ArrayList<IndicatorBreakdownDatabean>();
		//查找针对县区的比较
		List<IndicatorBreakdown> breakdownCountry = breakdownService.queryShoppingInd(filter);
		String area = "";
		java.text.DecimalFormat   df   =new   java.text.DecimalFormat("#.00");
		for (IndicatorBreakdown indicatorBreakdown : breakdownCountry) {
			IndicatorBreakdownDatabean databean = new IndicatorBreakdownDatabean();
			databean.setCumulative(df.format(Double.parseDouble(indicatorBreakdown.getCumulative())/10000));
			databean.setRatio(indicatorBreakdown.getRatio());
			if(indicatorBreakdown.getIndustryName().contains("地区生产")){
				databean.setIndustryName("GDP");
			}else {
				databean.setIndustryName(indicatorBreakdown.getIndustryName().substring(0, 4));
			}
			databean.setDate(indicatorBreakdown.getDate());
			if(!indicatorBreakdown.getArea().equals(filter.getArea())){
				area = indicatorBreakdown.getArea();
				breakdown3s.add(databean);
			}else{
				breakdown2s.add(databean);
			}
		}
		for (IndicatorBreakdownDatabean indicatorBreakdown : breakdown2s) {
			for (IndicatorBreakdownDatabean databean : breakdown3s) {
				if(indicatorBreakdown.getIndustryName().equals(databean.getIndustryName())){
					indicatorBreakdown.setOtherCum(databean.getCumulative());
					indicatorBreakdown.setOtherratio(databean.getRatio());
				}
			}
		}
		map.put("area1", filter.getArea());
		map.put("area2", area);
		map.put("data", breakdown2s);
		maps.add(map);
		//查找指标的比较
		breakdown2s = new ArrayList<IndicatorBreakdownDatabean>();
		breakdown3s = new ArrayList<IndicatorBreakdownDatabean>();
		List<IndicatorBreakdown> breakdowns = breakdownService.queryShopping(filter);
		for (IndicatorBreakdown indicatorBreakdown : breakdowns) {
			IndicatorBreakdownDatabean databean = new IndicatorBreakdownDatabean();
			databean.setCumulative(Double.toString(Double.parseDouble(indicatorBreakdown.getCumulative())/10000).substring(0, 5));
			databean.setRatio(indicatorBreakdown.getRatio());
			databean.setDate(indicatorBreakdown.getDate());
			String time="";
			if("2015".equals(indicatorBreakdown.getDate().substring(0, 4))){
				time="2016";
			}else if("2016".equals(indicatorBreakdown.getDate().substring(0, 4))){
				time="2017";
			}else{
				time=indicatorBreakdown.getDate().substring(0, 4);
			}
			if(indicatorBreakdown.getDate().contains("01-12")){
				databean.setDateName(time+"四季度");
			}else if(indicatorBreakdown.getDate().contains("01-09")){
				databean.setDateName(time+"三季度");
			}else {
				databean.setDateName(time+"上半年");
			}
			
			if(indicatorBreakdown.getArea().equals(filter.getArea())){
				breakdown2s.add(databean);
			}else {
				area = indicatorBreakdown.getArea();
				breakdown3s.add(databean);
			}
		}
		if(breakdown2s.size()>1){
			for (int i = 0; i < breakdown2s.size(); i++) {
				if(breakdown2s.get(i).getDate().contains("01-06")){
					continue;
				}
				Double double1 = Double.parseDouble(breakdown2s.get(i).getCumulative());
				Double double2 = Double.parseDouble(breakdown2s.get(i+1).getCumulative());
				breakdown2s.get(i).setCumulative(Double.toString(double1-double2).substring(0, 4));
			}
		}
		if(breakdown3s.size()>1){
			for (int i = 0; i < breakdown3s.size(); i++) {
				if(breakdown3s.get(i).getDate().contains("01-06")){
					continue;
				}
				Double double1 = Double.parseDouble(breakdown3s.get(i).getCumulative());
				Double double2 = Double.parseDouble(breakdown3s.get(i+1).getCumulative());
				breakdown3s.get(i).setCumulative(Double.toString(double1-double2).substring(0, 4));
			}
		}
		for (IndicatorBreakdownDatabean indicatorBreakdown : breakdown2s) {
			for (IndicatorBreakdownDatabean databean : breakdown3s) {
				if(indicatorBreakdown.getDateName().equals(databean.getDateName())){
					indicatorBreakdown.setOtherCum(databean.getCumulative());
					indicatorBreakdown.setOtherratio(databean.getRatio());
				}
			}
		}
		map = new HashMap<String, Object>();
		map2 = new HashMap<String, Object>();
		map.put("area1", filter.getArea());
		map.put("area2", area);
		map.put("data", breakdown2s);
		maps.add(map);
		return maps;
	}

}
